package dao;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import bean.AnnouncementBean;

/**
 * Class responsible for the operations of Announcement
 */ 
public class AnnouncementDAO implements DAO<AnnouncementBean> {
 
	private static final File DB = new File("./AnnouncementDB.xml");
	private static AnnouncementDAO instance;
	
	private AnnouncementDAO(){
		
	}
	
	
	/**
	 * Singleton 
         * Return instance of BlogDAO
	 * @return instance of BlogDAO
	 */
	
	public static synchronized AnnouncementDAO getInstance(){
		if(instance == null)
			instance = new AnnouncementDAO();
		
		return instance;
	}
	/**
         * Lists announcement
	 * @see dao.DAO#list()
	 */
	
	public List<AnnouncementBean> list() {
		XMLDecoder decoder = null;
		List<AnnouncementBean> list;
		try {
			decoder = new XMLDecoder(new BufferedInputStream(
					new FileInputStream(DB)));
			list = (List<AnnouncementBean>) decoder.readObject();
			return list;
		} catch (Exception e1) {
			return new ArrayList<AnnouncementBean>();
		} finally {
			if (decoder != null) {
				decoder.close();
			}
		}
	}

	/**
         * Delete announcement
         * @param bean - bean will be deleted
         * @return true, bean deleted
         *         false, bean not deleted
	 * @see dao.DAO#delete(dao.T)
	 */
	public boolean delete(AnnouncementBean bean) {
		List<AnnouncementBean> blogs = list();
		Iterator<AnnouncementBean> it = blogs.iterator();
		AnnouncementBean post;
		while (it.hasNext()) {
			post = it.next();
			if (bean.getId().equals(post.getId())) {
				blogs.remove(post);
				return writeList(blogs);
			}
				
		}
		return false;
	}
	
	 
	/**
         * Update of bean
         * @param bean - bean will be update
         * @return true, bean update
         *         false, bean not update
	 * @see dao.DAO#update(dao.T)
	 */
	public boolean update(AnnouncementBean bean) {
		List<AnnouncementBean> blogs = list();
		Iterator<AnnouncementBean> it = blogs.iterator();
		AnnouncementBean post;
		while (it.hasNext()) {
			post = it.next();
			if (bean.getId().equals(post.getId())) {
				blogs.remove(post);
				blogs.add(bean);
				return writeList(blogs);
			}
				
		}
		return false;
	}

	 
	/**
         * Save of bean
         * @param bean - bean will be save
         * @return true, bean save
         *         false, bean not save
	 * @see dao.DAO#save(dao.T)
	 */
	public boolean save(AnnouncementBean bean) {
		List<AnnouncementBean> list = list();
		list.add(bean);
		return writeList(list);
	}
 
	/**
         * Check if contains bean
         * @param bean - bean will be check
         * @return true, bean is contained
         *         false, bean is not contained
	 * @see dao.DAO#contains(dao.T)
	 */
	public boolean contains(AnnouncementBean bean) {
		List<AnnouncementBean> blogs = list();
		Iterator<AnnouncementBean> it = blogs.iterator();
		AnnouncementBean post;
		while (it.hasNext()) {
			post = it.next();
			if (bean.equals(post)) {
				return true;
				
			}
				
		}
		return false;
	}
	
        /**
         * Write liste 
         * @param list - list will be write
         * @return true, list was write
         *         false, list was not write
	 */
	private boolean writeList(List<AnnouncementBean> list) {
		XMLEncoder encoder = null;
		try {
			encoder = new XMLEncoder(new BufferedOutputStream(
					new FileOutputStream(DB)));
			encoder.writeObject(list);
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (encoder != null) {
				encoder.close();
			}
		}
		return false;
	}
	 
}
 
 